//
//  CaseProgressView.m
//  yonghu
//
//  Created by 浪尖渝力 on 2018/1/24.
//  Copyright © 2018年 com.DJS. All rights reserved.
//

#import "CaseProgressView.h"
#import "AJJDViewController.h"
@implementation CaseProgressView

-(void)creatView{
    self.backgroundColor=_LightGrey;
    _index=0;
    
    UIView *topView=[UIView new];
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(45);
        make.top.mas_equalTo(NAV_HEIGHT);
    }];
    topView.backgroundColor=[UIColor whiteColor];
    
    _topScrollView=[UIScrollView new];
    [topView addSubview:_topScrollView];
    [_topScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(-70);
        make.bottom.mas_equalTo(0);
    }];
    _topScrollView.scrollsToTop = NO;
    _topScrollView.contentSize = CGSizeMake(_titleArr.count*100, 0);
    _topScrollView.showsVerticalScrollIndicator = FALSE;
    _topScrollView.showsHorizontalScrollIndicator = FALSE;
    
    _labelArr=[NSMutableArray arrayWithCapacity:0];
    _xianView=[NSMutableArray arrayWithCapacity:0];
    for (int i=0; i<_titleArr.count; i++) {
        NSDictionary *dic=_titleArr[i];
        UIView *titleView=[UIView new];
        [_topScrollView addSubview:titleView];
        [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(i*100);
            make.width.mas_equalTo(100);
            make.top.mas_equalTo(0);
            make.height.mas_equalTo(45);
        }];
        
        UILabel *label=[UILabel new];
        [titleView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
        label.text=[dic objectForKey:@"name"];
        label.tag=i;
        label.textAlignment=NSTextAlignmentCenter;
        label.font=[UIFont systemFontOfSize:16];
        label.userInteractionEnabled=YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dianJi:)];
        [label addGestureRecognizer:tapGesture];
        
        CGSize size=[ZQTools getNSStringWeight:[dic objectForKey:@"name"] andViewWight:20 andFont:16];

        if (_index==i) {
            label.textColor=_backgroundColor;
        }
        else{
            label.textColor=ColorWithRGB(49, 49, 49);
        }
        [_labelArr addObject:label];
    }
    _scrollView=[UIScrollView new];
    [self addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_topScrollView.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-50);
    }];
    _scrollView.scrollsToTop = NO;
    _scrollView.contentSize = CGSizeMake(_mainW*_titleArr.count, 0);
    _scrollView.showsVerticalScrollIndicator = FALSE;
    _scrollView.showsHorizontalScrollIndicator = FALSE;
    _scrollView.pagingEnabled=YES;
    _scrollView.bounces = NO;
    _scrollView.delegate=self;
    _scrollView.tag=100;
    
    _tableArr=[NSMutableArray arrayWithCapacity:0];
    for (int i=0; i<_titleArr.count; i++) {
        UIScrollView *tableView=[UIScrollView new];
        [_scrollView addSubview:tableView];
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.width.mas_equalTo(_mainW);
            make.left.mas_equalTo(_mainW*i);
            make.height.mas_equalTo(_mainH-109);
        }];
        tableView.tag=i;
        tableView.backgroundColor=[UIColor whiteColor];
        [_tableArr addObject:tableView];
    }
    [self creatAnJianView];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.tag==100) {
        
        CGPoint points=scrollView.contentOffset;
        int pages=points.x/_mainW;
        _index=pages;
        
        if (pages<2) {
            _topScrollView.contentOffset=CGPointMake(0, 0);
        }
        else{
            _topScrollView.contentOffset=CGPointMake((pages-1)*100, 0);
        }
        for (int i=0; i<_titleArr.count; i++) {
            UILabel *label=_labelArr[i];
//            UIView *xianView=_xianView[i];
            if (i==pages) {
                label.textColor=_backgroundColor;
//                xianView.backgroundColor=_backgroundColor;
            }
            else{
                label.textColor=ColorWithRGB(49, 49, 49);
//                xianView.backgroundColor=[UIColor clearColor];
            }
            
        }
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.tag==100) {
        [self creatAnJianView];
    }
    
    
}
-(void)dianJi:(UITapGestureRecognizer *) tap{
    int abc=(int)tap.view.tag;
    _index=abc;
    if (abc<2) {
        _topScrollView.contentOffset=CGPointMake(0, 0);
    }
    else{
        _topScrollView.contentOffset=CGPointMake((abc-1)*100, 0);
    }
    _scrollView.contentOffset=CGPointMake(abc*_mainW, 0);
    for (int i=0; i<_titleArr.count; i++) {
        UILabel *label=_labelArr[i];
        if (i==abc) {
            label.textColor=_backgroundColor;
        }
        else{
            label.textColor=ColorWithRGB(49, 49, 49);
        }
        
    }
    [self creatAnJianView];
    
}

-(void)creatAnJianView{
    
    UIScrollView *tableView=_tableArr[_index];
    NSDictionary *Dic=_titleArr[_index];
    NSArray *typeArr=[Dic objectForKey:@"children"];
    if (typeArr==nil||typeArr.count==0) {
        return;
    }
    int abc=(int)typeArr.count;
    
    tableView.contentSize = CGSizeMake(0,abc*170*1.3+150);
    
    UIView *view=[UIView new];
    [tableView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(abc*170*1.3+150);
        make.width.mas_equalTo(_mainW);
    }];
    
   
    
    for (int i=0; i<typeArr.count; i++) {
        
        UIImageView *imageView=[UIImageView new];
        [view addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i%2==1) {
                 make.right.mas_equalTo(-_mainW/2);
            }if (i%2==0) {
                make.left.mas_equalTo(_mainW/2);
            }
            make.bottom.mas_equalTo(-100-163*1.3*i);
            make.height.mas_equalTo(167*1.3);
            make.width.mas_equalTo(80*1.3);
        }];
        if (i%2==1) {
            if (i==abc-1) {
                imageView.image=[UIImage imageNamed:@"半圆1"];
            }else{
                imageView.image=[UIImage imageNamed:@"半圆3"];
            }
            
        }if (i%2==0) {
            if (i==abc-1) {
                imageView.image=[UIImage imageNamed:@"半圆1-拷贝"];
            }else{
                imageView.image=[UIImage imageNamed:@"半圆2"];
            }
        }
            UIView *view2=[UIView new];
            [view addSubview:view2];
            [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(imageView);
                make.width.height.mas_equalTo(100);
                if (i%2==1) {
                    make.centerX.mas_equalTo(imageView.mas_left);
                }if (i%2==0) {
                    make.centerX.mas_equalTo(imageView.mas_right);
                }
            }];
            view2.layer.cornerRadius=50;
            if (i%2==1) {
                view2.backgroundColor=[UIColor orangeColor];
            }if (i%2==0) {
                view2.backgroundColor=_tianLanColor;
            }if (i==abc-1) {
                view2.backgroundColor=ColorWithRGB(248, 60, 78);
            }

        UILabel *label=[UILabel new];
        [view2 addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
        label.numberOfLines=0;
        NSDictionary *dic=typeArr[i];
        NSNumber *schedule_time1=[dic objectForKey:@"schedule_time"];
        NSString *schedule_time=[NSString stringWithFormat:@"%lld",schedule_time1.longLongValue];
        label.textColor=[UIColor whiteColor];
        label.font=[UIFont systemFontOfSize:14];
        label.textAlignment=NSTextAlignmentCenter;
        label.text=[NSString stringWithFormat:@"%@\n%@",[ZQTools changeTimeCuo:@"yyyy.MM.dd" :schedule_time],[ZQTools changeTimeCuo:@"HH:mm" :schedule_time]];
        
        UIView *view3=[UIView new];
        [view addSubview:view3];
        [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(160);
            make.centerY.mas_equalTo(view2);
            if (i%2==1) {
                make.left.mas_equalTo(imageView.mas_right);
            }if (i%2==0) {
                make.right.mas_equalTo(imageView.mas_left);
            }
        }];
        NSString *schedule_title=[dic objectForKey:@"schedule_title"];
        NSString *schedule_content=[dic objectForKey:@"schedule_content"];
        NSArray *img=[dic objectForKey:@"img"];
        view3.backgroundColor=_LightGrey;
        
        UILabel *labe2=[UILabel new];
        [view3 addSubview:labe2];
        [labe2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            if (i%2==1) {
                make.left.mas_equalTo(5);
            }if (i%2==0) {
                make.right.mas_equalTo(-5);
            }
        }];
        labe2.text=schedule_title;
        labe2.font=[UIFont systemFontOfSize:16];
        labe2.textColor=view2.backgroundColor;
        
        if ((img==nil||img.count==0)&&![ZQTools charIsNil:schedule_content]) {
            view3.tag=i;
            view3.userInteractionEnabled=YES;
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToAJJD:)];
            [view3 addGestureRecognizer:tapGesture];
            
            CGSize size=[ZQTools getNSStringSize:schedule_content andViewWight:110 andFont:14];
            [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(120);
                if (size.height>120) {
                    make.height.mas_equalTo(160);
                }else{
                    make.height.mas_equalTo(size.height+70);
                }
                make.centerY.mas_equalTo(view2);
                if (i%2==1) {
                    make.left.mas_equalTo(imageView.mas_right);
                }if (i%2==0) {
                    make.right.mas_equalTo(imageView.mas_left);
                }
            }];
            UILabel *label3=[UILabel new];
            [view3 addSubview:label3];
            [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(5);
                make.right.mas_equalTo(-5);
                make.top.mas_equalTo(labe2.mas_bottom).mas_equalTo(5);
            }];
            label3.numberOfLines=6;
            label3.textColor=ColorWithRGB(149, 149, 149);
            label3.font=[UIFont systemFontOfSize:14];
            label3.text=schedule_content;
        }
        if ((img!=nil&&img.count!=0)&&[ZQTools charIsNil:schedule_content]) {
            NSDictionary *imgDic=img[0];
            view3.tag=i;
            view3.userInteractionEnabled=YES;
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToAJJD2:)];
            [view3 addGestureRecognizer:tapGesture];
            
            UIImageView *imageView4=[UIImageView new];
            [view3 addSubview:imageView4];
            [imageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.top.mas_equalTo(labe2.mas_bottom).mas_equalTo(5);
            }];
            NSString *si_img_url=[imgDic objectForKey:@"si_thum_url"];
            NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",_addressUrl,si_img_url]];
            [imageView4 sd_setImageWithURL:url];
            
            UIView *view4=[UIView new];
            [view3 addSubview:view4];
            [view4 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.height.mas_equalTo(25);
            }];
            view4.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
            
            UILabel *label5=[UILabel new];
            [view4 addSubview:label5];
            [label5 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(0);
            }];
            label5.text=[NSString stringWithFormat:@"共%d张",img.count];
            label5.textColor=ColorWithRGB(1, 1, 1);
            label5.font=[UIFont systemFontOfSize:15];
        }
        if ((img!=nil&&img.count!=0)&&![ZQTools charIsNil:schedule_content]) {
            NSDictionary *imgDic=img[0];
            view3.tag=i;
            view3.userInteractionEnabled=YES;
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToAJJD3:)];
            [view3 addGestureRecognizer:tapGesture];
            
            UIImageView *imageView4=[UIImageView new];
            [view3 addSubview:imageView4];
            [imageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.top.mas_equalTo(40);
                make.height.mas_equalTo(80);
            }];
            NSString *si_img_url=[imgDic objectForKey:@"si_thum_url"];
            NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",_addressUrl,si_img_url]];
            [imageView4 sd_setImageWithURL:url];
            
            UIView *view4=[UIView new];
            [view3 addSubview:view4];
            [view4 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.bottom.mas_equalTo(imageView4);
                make.height.mas_equalTo(25);
            }];
            view4.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
            
            UILabel *label5=[UILabel new];
            [view4 addSubview:label5];
            [label5 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.mas_equalTo(0);
            }];
            label5.text=[NSString stringWithFormat:@"共%d张",img.count];
            label5.textColor=ColorWithRGB(1, 1, 1);
            label5.font=[UIFont systemFontOfSize:15];
            
            UILabel *label3=[UILabel new];
            [view3 addSubview:label3];
            [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(5);
                make.right.mas_equalTo(-5);
                make.top.mas_equalTo(imageView4.mas_bottom).mas_equalTo(5);
            }];
            label3.numberOfLines=2;
            label3.textColor=ColorWithRGB(149, 149, 149);
            label3.font=[UIFont systemFontOfSize:14];
            label3.text=schedule_content;
        }
    }
    UIImageView *imageView2=[UIImageView new];
    [view addSubview:imageView2];
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.height.width.mas_equalTo(100);
        make.bottom.mas_equalTo(-70);
    }];
    imageView2.layer.masksToBounds=YES;
    imageView2.layer.cornerRadius=50;
    imageView2.image=[UIImage imageNamed:@"圆logo-"];
    
}
-(void)pushToAJJD:(UITapGestureRecognizer *)tap{
    NSDictionary *typeDic=_titleArr[_index];
    NSArray *typeArr=[typeDic objectForKey:@"children"];
    AJJDViewController *contr=[[AJJDViewController alloc] init];
    contr.dic=typeArr[tap.view.tag];
    contr.abc=1;
    [ZQTools pushNextViewController:self.viewController andRootController:contr];
}
-(void)pushToAJJD2:(UITapGestureRecognizer *)tap{
    NSDictionary *typeDic=_titleArr[_index];
    NSArray *typeArr=[typeDic objectForKey:@"children"];
    AJJDViewController *contr=[[AJJDViewController alloc] init];
    contr.dic=typeArr[tap.view.tag];
    contr.abc=2;
    [ZQTools pushNextViewController:self.viewController andRootController:contr];
}
-(void)pushToAJJD3:(UITapGestureRecognizer *)tap{
    NSDictionary *typeDic=_titleArr[_index];
    NSArray *typeArr=[typeDic objectForKey:@"children"];
    AJJDViewController *contr=[[AJJDViewController alloc] init];
    contr.dic=typeArr[tap.view.tag];
    contr.abc=3;
    [ZQTools pushNextViewController:self.viewController andRootController:contr];
}


@end
